/**
 * Copyright 1998-2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
package ca.uvic.cs.cogz.mapping.filter;

import java.util.HashSet;

import ca.uvic.cs.cogz.mapping.filter.TreeFilterManager.TreeFilterType;
import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Frame;

/**
 * Implementation of a ITreeFilter. Used to filter or un-filter specific branches of the ontology.
 * 
 * @author Sean Falconer
 * @date 14-March-08
 */
public class FrameItemFilter implements ITreeFilter {
	private HashSet<Frame> filteredItems;
	private HashSet<Frame> nonFilteredItems;
	private HashSet<Frame> pinnedItems;

	public FrameItemFilter() {
		filteredItems = new HashSet<Frame>();
		nonFilteredItems = new HashSet<Frame>();
		pinnedItems = new HashSet<Frame>();
	}

	public void addFilteredItem(Frame frameToFilter) {
		filteredItems.add(frameToFilter);
	}

	public void addNonFilteredItem(Frame frameToNotFilter) {
		nonFilteredItems.add(frameToNotFilter);
		nonFilteredItems.addAll(((Cls) frameToNotFilter).getDirectSubclasses());
		pinnedItems.add(frameToNotFilter);
	}

	/**
	 * A frame is a match if it is not contained in our filter list.
	 */
	public boolean isMatch(Frame frame) {
		return isActive() && (!filteredItems.contains(frame) || nonFilteredItems.contains(frame));
	}

	public void removePin(Frame frame) {
		pinnedItems.remove(frame);
		nonFilteredItems.remove(frame);
		nonFilteredItems.removeAll(((Cls) frame).getDirectSubclasses());
	}

	public boolean isPinned(Frame frame) {
		return pinnedItems.contains(frame);
	}

	public boolean isActive() {
		return filteredItems.size() > 0 || nonFilteredItems.size() > 0;
	}

	public void reset() {
		filteredItems.clear();
		nonFilteredItems.clear();
		pinnedItems.clear();
	}

	public TreeFilterType getType() {
		return TreeFilterType.FRAME_ITEM_FILTER;
	}
}
